/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.content.Context;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.SimpleKeyValueBean;
import com.naizhuai.ui.entity.mainbusiness.TargetBean;
import com.naizhuai.util.DeviceUtils;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.NaizhuAILog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ActionAddImageAdapterHolder extends RecyclerView.Adapter {

    public static final int URL_NET_IMAGE = 0;
    public static final int LOCAL_FILE_IMAGE = 1;

    public static final int FILE_SIZE_TOTAL = 3;

    private OnItemClickListener mItemClickListener;
    private BaseActivity mContext;
    private List<SimpleKeyValueBean> filePathStrs;
    private int pagerWidth;
    private boolean scanMode=true;

    public static final int ITEM_IMAGE_CONTENT = 0;
    public static final int ITEM_ADD_ICON = 1;


    public ActionAddImageAdapterHolder(BaseActivity context, List<SimpleKeyValueBean> filePathStrs, int pagerWidth) {
        this.mContext = context;
        this.filePathStrs = filePathStrs;
        this.pagerWidth = pagerWidth;
    }

    public void setScanMode(boolean scanMode){
        this.scanMode=scanMode;
    }

    /**
     * 设置不同的类型：
     * 0：表示展示的title的item
     * 1：展示的item内容
     *
     * @param position item位置
     * @return position
     */
    @Override
    public int getItemViewType(int position) {
        if (position < filePathStrs.size())
            return ITEM_IMAGE_CONTENT;
        else
            return ITEM_ADD_ICON;
    }


    @Override
    public int getItemCount() {
        return FILE_SIZE_TOTAL;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_IMAGE_CONTENT) {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_action_image_details, parent, false);
            return new ActionImageItemHolder(sView);
        } else {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_action_image_details_add, parent, false);
            return new ActionAddItemHolder(sView);
        }
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        holder.itemView.post(new Runnable() {
            @Override
            public void run() {
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
                int itemWidth = (pagerWidth - params.leftMargin * 4) / 3;
                params.height = itemWidth;
                params.width = itemWidth;
                holder.itemView.setLayoutParams(params);
                initItemView(holder, position);
            }
        });
    }

    private void initItemView(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ActionImageItemHolder) {
            ActionImageItemHolder imageHolder = (ActionImageItemHolder) holder;

            SimpleKeyValueBean simpleValue = filePathStrs.get(position);
            String filePath = filePathStrs.get(position).getValue();
            if (simpleValue.getKey() == LOCAL_FILE_IMAGE) {
                File file = new File(filePath);
                ImageUtils.glideImageViewByFile(mContext,imageHolder.image,file);
            } else if (simpleValue.getKey() == URL_NET_IMAGE) {
                ImageUtils.glideImageViewByUrl(mContext,imageHolder.image,filePath);
            }

        } else if (holder instanceof ActionAddItemHolder) {
            ActionAddItemHolder addHolder = (ActionAddItemHolder) holder;
            addHolder.imageAdd.setEnabled(!scanMode);
        }
    }


    public class ActionImageItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView image;

        public ActionImageItemHolder(View view) {
            super(view);
            image = (ImageView) view.findViewById(R.id.image_details);
            view.getLayoutParams().width = DeviceUtils.getScreenDevice(mContext)[0] / 3;
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null)
                mItemClickListener.onItemClick(v, getAdapterPosition(), ITEM_IMAGE_CONTENT);
        }
    }


    public class ActionAddItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView imageAdd;
        TextView imageAddTxt;

        public ActionAddItemHolder(View view) {
            super(view);
            imageAdd = view.findViewById(R.id.image_add);
            imageAddTxt = view.findViewById(R.id.image_add_txt);
            view.getLayoutParams().height = DeviceUtils.getScreenDevice(mContext)[0] / 3;
            imageAdd.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null){
                mItemClickListener.onItemClick(v, getAdapterPosition(), ITEM_ADD_ICON);
            }
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position, int indexType);
    }

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
